import * as liviinghomeApi from '@/api/liviinghome.js';

export default {
  namespaced: true,
  state() {
    return {
      liviinghomeList: {},
      filterList: {
        brands: [],
        categories: [],
        goods: [],
        saleProperties: [],
      },
      subgoodsList: {
        items: [],
        page: 1,
        pageSize: 10,
        counts: 0,
        pages: 1,
      },
      productList: {
        mainPictures: [],
        specs: [],
        skus: [],
        details: [],
      },
      subBannerList: [],
      subHotSaleList: {},
      subWeekHotList: {},
    };
  },
  getters: {
    // 加品牌
    subFilterList(state) {
      return [
        {
          id: 'pinpai',
          name: '品牌',
          properties: state.filterList.brands,
        },
        ...state.filterList.saleProperties,
      ];
    },
    subBanner(state) {
      // 每四个一组
      const arr = [];
      for (let i = 0; i < state.subBannerList.length; i += 4) {
        arr.push(state.subBannerList.slice(i, i + 4));
      }
      return arr;
    },
  },
  mutations: {
    // 分类
    SET_LIVIING(state, newValue) {
      state.liviinghomeList = newValue;
    },
    // 二级分类茶咖酒具
    SET_FILTER(state, newValue) {
      state.filterList = newValue;
    },
    // 懒加载拼接数据
    SET_SUBGOODS(state, newValue) {
      state.subgoodsList = {
        ...newValue,
        items: state.subgoodsList.items.concat(newValue.items),
      };
    },
    // 重置二级分类茶咖酒具图片集数据
    RESET_SUBGOODS(state, newValue) {
      state.subgoodsList = newValue;
    },
    // 产品详情
    SET_PRODUCT(state, newValue) {
      state.productList = newValue;
    },
    // 轮播图数据
    SET_SUB_BANNER(state, newValue) {
      state.subBannerList = newValue;
    },
    // 24小时热销榜
    SET_SUB_HOT_SALE(state, newValue) {
      state.subHotSaleList = newValue;
    },
    // 周销榜
    SET_SUB_WEEK_HOT(state, newValue) {
      state.subWeekHotList = newValue;
    },
  },
  actions: {
    // 分类
    async updateLivingHome({ commit }, id) {
      const res = await liviinghomeApi.livinghome(id);
      commit('SET_LIVIING', res);
    },
    // 二级分类茶咖酒具
    async updateFilter({ commit }, id) {
      const res = await liviinghomeApi.filter(id);
      commit('SET_FILTER', res);
    },
    // 二级分类茶咖酒具图片集
    async updateSubGoods({ commit }, params) {
      const res = await liviinghomeApi.subgoods(params);
      commit('SET_SUBGOODS', res);
    },
    // 重置二级分类茶咖酒具图片集数据
    async resetSubGoods({ commit }, params) {
      const res = await liviinghomeApi.subgoods(params);
      commit('RESET_SUBGOODS', res);
    },
    // 产品详情
    async updateProduct({ commit }, id) {
      const res = await liviinghomeApi.product(id);
      commit('SET_PRODUCT', res);
    },
    // 轮播图数据
    async updateSubBanner({ commit }, params) {
      const res = await liviinghomeApi.subbanner(params);
      commit('SET_SUB_BANNER', res);
    },
    // 24小时热销榜
    async updateSubHotSale({ commit }, params) {
      const res = await liviinghomeApi.subhotsale(params);
      commit('SET_SUB_HOT_SALE', res);
    },
    // 周销榜
    async updateSubWeekHot({ commit }, params) {
      const res = await liviinghomeApi.subhotsale(params);
      commit('SET_SUB_WEEK_HOT', res);
    },
  },
};
